<template>
  <!-- 实现页面的基本布局 -->
  <el-card class="tree-card">
    <!-- 用了一个行列布局 -->
    <!-- 头部 -->
    <tree-tools :tree-node="company" :is-root="true" @addDepts="addDepts" />
    <!-- 树形控件 -->
    <el-tree :data="departs" :props="defaultProps">
      <!-- 插槽内容会循环多次 -->
      <tree-tools slot-scope="{data}" :tree-node="data" @delDepts="getDepartments" @addDepts="addDepts" @editDepts="editDepts" />
    </el-tree>
    <!-- 新增组件 -->
    <add-depts ref="addDepts" :dialog-show.sync="dialogShow" :tree-node="node" @addDepts="getDepartments" />
  </el-card>
</template>

<script>
import TreeTools from '@/views/departments/components/tree-tools.vue'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index.js'
import AddDepts from './components/add-depts.vue'
export default {
  components: {
    TreeTools,
    AddDepts
  },
  data() {
    return {
      defaultProps: {
        label: 'name'
      },
      departs: [], // 组织架构节点数据
      company: { }, // 头部
      dialogShow: false, // 控制弹窗的显示隐藏
      node: null// 记录当前点击的节点
    }
  },
  created() {
    this.getDepartments()// 调用接口方法
  },
  methods: {
    // 编辑部门
    editDepts(treeNode) {
      this.dialogShow = true
      this.node = treeNode// 拿到当前操作的节点
      this.$refs.addDepts.getDepartmentsDetail(treeNode.id)
    },
    // 新增部门
    addDepts(treeNode) {
      this.dialogShow = true
      this.node = treeNode// 拿到当前操作的节点
    },
    // 封装接口存储数据方法
    async getDepartments() {
      const res = await getDepartments()// 调用接口拿到数据
      // console.log(res)
      this.departs = tranListToTreeData(res.depts, '')// 将接口返回的数据,递归处理之后赋值给树形控件变量depts
      this.company = { name: res.companyName, manager: '负责人', id: '' }
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
